var comPageNum = 0;//分页公共页码
var isEdit = false;//是否编辑--判断是否跟新页码到首页
var html=null;
var code=''
$("#form-member").validate({
    rules: {
        category_id: {
            maxlength:10,
            minlength:10,
        },
        logmax: {
            maxlength:30,
        },
        logmin: {
            maxlength:10,
            minlength:10,
        },
        shaixuan: {
            maxlength:10,
            minlength:10,
        }
    },
    //提交表单数据
    submitHandler:function(form){
        queryDataFn();
    }
})
function queryDataFn() {
    /* 校验配置内容 */
    if ($("#category_id").val()==0){
        layer.msg('请选择配置内容!', {icon: 6, time: 3000});
        return;
    }
    //获取
    var category_id=$("#category_id").val(),
        logmax=$("#logmax").val(),
        logmin=$("#logmin").val(),
        shaixuan=$("#shaixuan").val(),
        titlee=$("#text").val(),
        pingtai=$("#pingtai").find("option:selected").val(),
        cangku=code
    //提交
    var user_data = {
        categoryId:category_id,
        endTime:logmax,
        startTime:logmin,
        status:shaixuan,
        title:titlee,
        platform:pingtai,
        warehouseCode:cangku,
        nowTime:new Date().Format("yyyy-MM-dd hh:mm:ss")
    }
    //获取返回的数据
    var succCallBack = function (data, status, response) {
        if(data.errorCode=="0") {
            var dataSource = new Array();
            dataSource = [];
            var result=data.data
            $.each(result, function (i, field){
                var rowData = new Array();
                //平台-0
                rowData.push(field.platform);
                //地域-1
                rowData.push(field.warehouseName);
                //活动标题-2
                rowData.push(field.title);
                //生效时间-3
                rowData.push(field.startTime);
                //失效时间-4
                rowData.push(field.endTime);
                //状态-5
                rowData.push(field.status);
                //展示顺序-6
                rowData.push(field.displaySequence);
                //创建人-7
                rowData.push(field.createdName);
                //创建时间-8
                rowData.push(field.createdAt);
                //创建人-9
                rowData.push(field.categoryId);
                //创建时间-10
                rowData.push(field.contentManagementId);
                dataSource.push(rowData);
            });
            initDataTable(dataSource);
            return false
        }else{
            //报错
            var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
            dttable.fnClearTable(); //清空一下table
            return false
        }
    };
    //给一个接口地址提交数据
    return web.doAjax(root.sroreinter.search_content_conf, 'post', user_data , succCallBack,1);
    //获取数据之后如何展现在页面上
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "fnDrawCallback" : function(){
                if(!isEdit){
                    comPageNum = getDataTablePageNum(dttable);
                }
                this.api().column(0).nodes().each(function(cell,i){
                    cell.innerHTML = i+1;
                    //权限控制函数
                    power()
                });
            },
            "autoWidth": false,
            "order": [[ 9, "desc" ]],
            "ordering": true,
            "searching": true,
            "paging": true,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu":[10,20,30],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {"data":null,"targets":0 },
                {
                    "render": function(data,type,row,meta){
                        var val=""
                        if (row[0]==10){
                            val='签约平台'
                        }else if (row[0]=='20'){
                            val='普通平台'
                        }else if (row[0]=='10,20'){
                            val='签约平台/普通平台'
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[3];
                        if (date=='1000-01-01 00:00:00'){
                            val=''
                            return val;
                        }
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets":4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[4];
                        if (date=='1000-01-01 00:00:00'){
                            val=''
                            return val;
                        }
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[5]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[6]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[7]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 8
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[8]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 9
                },
                {
                    "render": function(data,type,row,meta){
                        var val ='<a class="powerButton 210"><i class="Hui-iconfont mr-10" onclick="edit_data('+row[10]+','+row[9]+')">&#xe6df;</i></a> <a class="powerButton 211"><i class="Hui-iconfont" onclick="delete_data('+row[10]+')">&#xe6e2;</i></a>';
                        return val;
                    },
                    "targets": 10
                },
            ]
        });
        dttable.fnPageChange(comPageNum);
        isEdit = false;
        //权限控制函数
        power()
    };
}
function changeCang() {
    code=''
    var warcode=""
    var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
    $.each(warehouses.data,function(k,s){
        if(document.getElementById('yun'+s.warehouseCode+'').checked==true){//获取选中的id
            warcode+=document.getElementById('yun'+s.warehouseCode+'').value+ ","//逗号拼接获取到的所以id
        }
    })
    //去掉最后一位的逗号
    if (warcode.length > 0) {
        warcode = warcode.substr(0, warcode.length - 1);
    }
    console.log(warcode)
    code=warcode
}
function delete_data(id){
    layer.confirm('你确定要删除吗？',function(index){
        //此处请求后台程序，下方是成功后的前台处理……
        $.ajax({
            type: "post",
            url: root.sroreinter.delete_content_conf,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                content_management_id:id,
            },
            //传入组装的参数
            success: function (data,user_data,succCallBack) {
                console.log(data)
                if(data.errorCode=="0") {
                    layer.msg('操作成功!', {icon: 6, time: 1000});
                    setTimeout( function () {
                        isEdit = true;
                        queryDataFn();
                    },700);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                 alert(XMLHttpRequest);
            }
        })
    });
}
function edit_data(content_management_id,category_id){
    var top
    if (category_id=='15'){
        top="方盛头条编辑配置"
    }else if(category_id=='16'){
        top="登录页页尾编辑配置"
    }else{
        top="首页页尾编辑配置"
    }
    var index = layer.open({
        type: 2,
        title:top,
        content: "content_edit.html"+"?content_management_id="+content_management_id
    });
    layer.full(index);
}

/*
下拉框的展示
 */
$(function () {
    $.ajax({
        type: "post",
        url: root.sroreinter.get_content_category,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            category_id:16,type:104
        },  //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#category_id").html(interText(dataInter));
            }
        }
    })
})
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}